package com.lzc.tlmail.tlmailseckill.interceptor;


import com.lzc.common.constant.OauthConstant;
import com.lzc.common.vo.MemberEntityVo;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


/**
 * @author lzc
 * @create 2020-10-18 15:50
 */
public class SeckillInterceptor implements HandlerInterceptor {

    public static ThreadLocal<MemberEntityVo> userInfotoThreadLocal=new ThreadLocal<>();

    /**
     * 目标方法之前执行
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request,
                             HttpServletResponse response, Object handler) throws Exception {
        String uri = request.getRequestURI();
        boolean match = new AntPathMatcher().match("/kill", uri);
        if(match){

            HttpSession session = request.getSession();
            MemberEntityVo memberEntityVo= (MemberEntityVo) session.getAttribute(OauthConstant.OAUTH_LOGINUSER);
            if(memberEntityVo!=null){
                userInfotoThreadLocal.set(memberEntityVo);
                return true;
            }else {
                session.setAttribute("msg","尚未登录请先登录");
                response.sendRedirect("http://www.auth.tlmail.com/login.html");
                return false;
            }
        }
        return true;
    }


}
